﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using AspectCore.DynamicProxy;
using AspectCore.Configuration;
using AspectCore.Extensions;
using AspectCore.Injector;
using Newtonsoft.Json;
using Newtonsoft;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Serialization;
using System.IO;
using AutoMapper;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args) {
            var factory = new ConnectionFactory();
            factory.HostName = "10.10.10.45";//RabbitMQ服务在本地运行
            factory.UserName = "root";//用户名
            factory.Password = "123123";//密码

            using (var connection = factory.CreateConnection()) {
                using (var channel = connection.CreateModel()) {
                    channel.QueueDeclare("hello", false, false, false, null);

                    var consumer = new EventingBasicConsumer(channel);
                    channel.BasicConsume("hello", false, consumer);
                    consumer.Received += (model, ea) => {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine("已接收： {0}", message);
                        channel.BasicAck(ea.DeliveryTag, false);
                    };
                    Console.ReadLine();
                }
            }

            //Mapper.Initialize(r => {
            //    r.CreateMap<TestUser, TestUserDto>();
            //    r.CreateMap<Student, StudentDto>();
            //});


            //Student s1 = new Student(new { aaa = "aaa", bbb = DateTime.Now }) {
            //    className = "1年级1班",
            //    user = new TestUser {
            //        id = Guid.NewGuid(),
            //        age = 18,
            //        name = "laowang"
            //    }
            //};

            //StudentDto s2 = new StudentDto(s1.DynamicData) {
            //    user_name = s1.user.name,
            //    user = new TestUserDto {
            //        age = s1.user.age,
            //        name = s1.user.name
            //    }
            //};

            //Console.WriteLine(JsonConvert.SerializeObject(s2));
        }
    }

    public class DynamicDtobase : Dictionary<string, object>
    {
        public DynamicDtobase(string json) {
            var value = JsonConvert.DeserializeObject<JObject>(json);
            foreach (var item in value) {
                this.Add(item.Key, item.Value);
            }
        }
    }


    public class StudentDto : DynamicDtobase
    {
        public StudentDto(string json) : base(json) {
        }

        public TestUserDto user { get; set; }

        public string user_name { get; set; }
    }

    public class TestUserDto
    {
        public string name { get; set; }
        public int age { get; set; }
    }

    public class TestUser
    {
        public Guid id { get; set; }

        public string name { get; set; }

        public int age { get; set; }
    }

    public class Student : DynamicEntityBase
    {
        public Student(object dynamicData) : base(dynamicData) {
        }

        public string className { get; set; }

        public TestUser user { get; set; }
    }

    public abstract class DynamicEntityBase
    {
        public DynamicEntityBase(object dynamicData) {
            DynamicData = JsonConvert.SerializeObject(dynamicData);
        }

        public string DynamicData { get; set; }
    }
}
